<?php

namespace App\Http\Controllers\Business;

use App\Common\Traits\Model\ModelSearchTrait;
use App\Http\Controllers\Controller;
use App\Models\Business\ServiceCategory;
use Illuminate\Http\Request;

class ServiceCategoryController extends Controller
{
    use ModelSearchTrait;

    //
    public function index(Request $request,ServiceCategory $category)
    {
        $category=$this->singleModelSearch($request,$category,$this->serviceCondition());
        $categories=$category->orderByDesc('updated_at')->paginate($request->size??20)->withQueryString();
        return $this->success($categories);
    }
    //
    public function serviceCondition()
    {
        return [
            ['service_name','like'],
            ['category_name','like'],
        ];
    }

    //
    public function store(Request $request)
    {
        if(!$request->filled('service_name') || !$request->filled('category_name')){
            return $this->failed('服务项目和服务类型都不能为空',201);
        }
        $data=[
            'service_name'=>mb_substr($request->service_name,0,64),
            'category_name'=>mb_substr($request->category_name,0,64),
        ];
        ServiceCategory::create($data);
        return $this->success(['msg'=>'创建成功']);
    }


    //
    public function show($id)
    {
        $serviceCategory=ServiceCategory::find($request->category_id??0);
        return $this->success($serviceCategory);
    }

    //
    public function update(Request $request,ServiceCategory $category)
    {
        if(!$request->filled('service_name') || !$request->filled('category_name')){
            return $this->failed('服务项目和服务类型都不能为空',201);
        }
        $serviceCategory=$category->find($request->category_id??0);
        if(empty($serviceCategory)){
            return $this->failed('id错误，无此服务项目',201);
        }
        $data=[
            'service_name'=>$request->service_name,
            'category_name'=>$request->category_name,
        ];
        $serviceCategory->update($data);
        return $this->success(['msg'=>'编辑成功']);
    }

    //
    public function destroy(Request $request,ServiceCategory $category)
    {
        $serviceCategory=$category->find($request->category_id??0);
        if(empty($serviceCategory)){
            return $this->failed('id错误，无此服务项目',201);
        }
        $serviceCategory->delete();
        return $this->success(['msg'=>'删除成功']);
    }

    //
    public function getAllCategories(){
        $cates=ServiceCategory::all();
        return $this->success($cates) ;
    }

}
